/*
* @Author: Josh
* @Date:   2019-01-26 15:24:42
* @Last Modified by:   Administrator
* @Last Modified time: 2019-04-14 15:01:27
*/
'use steict'
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');
var navSide       = require('page/common/nav-side/index.js'),
    _mall         = require('util/mall.js'),
    _user         = require('service/user-service.js'),
    templateIndex = require('./index.string');

//page 逻辑部分
var page = {
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        //初始化左侧菜单
        navSide.init({
            name : 'user-center'
        });
        //加载用户信息
        this.loadUserInfo()
    },
    bindEvent : function(){
        var _this = this;
        //修改个人信息
        $('#submit').click(function(){
            var userInfo = {
                    phone    : $.trim($('#phone').val()),
                    email    : $.trim($('#email').val()),
                    question : $.trim($('#question').val()),
                    answer   : $.trim($('#answer').val())
                },
                validateResult = _this.validateForm(userInfo);

            if(validateResult.status){
                //更改用户信息
                _user.updateUserInfo(userInfo, function(res, msg){
                    _mall.successTips(msg);
                    window.location.href = './user-center.html';
                }, function(errMsg){
                    _mall.errorTips(errMsg);
                });
            }
            else{
                _mall.errorTips(validateResult.msg);
            }
        });
    },
    //验证字段信息
    validateForm : function(formData){
        var result = {
            status : false,
            msg    : '',
        };
        //验证手机号码
        if(!_mall.validate(formData.phone, 'phone')){
            result.msg = '请输入正确的手机号码！';
        }
        //验证邮箱号码
        else if(!_mall.validate(formData.email, 'email')){
            result.msg = '请输入正确的邮箱号码！';
        }
        //验证密码找回问题是否为空
        else if(!_mall.validate(formData.question, 'require')){
            result.msg = '请输入密码找回问题！';
        }
        ////验证密码找回问题答案是否为空
        else if(!_mall.validate(formData.answer, 'require')){
            result.msg = '请输入密码找回问题答案！';
        }
        else{
            //通过验证，返回正确提示
            result.status = true;
            result.msg    = '验证成功';
        }
        return result;
    },
    //加载用户信息
    loadUserInfo : function(){
        var userHtml = '';
        _user.getUserInfo(function(res){
            userHtml = _mall.renderHtml(templateIndex, res);
            $('.panel-body').html(userHtml);
            $('.panel').addClass('onLogin');
        }, function(errMsg){
            _mall.errorTips(errMsg);
        });
    }
};
    
$(function(){
    page.init(); 
});